package com.liuya.safe.entitle.service;

import com.liuya.common.exception.EntityExistException;
import com.liuya.entitle.*;
import com.liuya.safe.model.SafeDecisionEntitlement;
import com.liuya.safe.model.SafeQueryEntitlement;
import com.liuya.safe.model.SafeUser;
import com.liuya.safe.model.SafeQuery;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public interface EntitleManagerService extends Serializable {

	/**
	 * 
	 * @param pvlgId
	 * @return Collection< {@link DecisionEntitlement} >
	 */
	public Collection getDecisionEntitlements(String pvlgId);

	/**
	 * 
	 * @param pvlgId
	 * @return Collection< {@link QueryEntitlement} >
	 */
	public Collection getQueryEntitlements(String pvlgId);

	/**
	 * Check this user has fine grained(data level) access to this given data(in
	 * context). If this privilege is a normal one, check this user has function
	 * access to this privilege first!
	 * 
	 * @param locale
	 * @param pvlgId
	 * @param user
	 * @param ctx
	 * @return
	 */
	public Decision permit(Locale locale, String pvlgId, SafeUser user, Map ctx);

	/**
	 * Execute for this user, return fine grained(data level, row and column
	 * level) data. If this user has no privilege to execute privilege, return
	 * empty Collection.
	 * 
	 * @param pvlgId
	 * @param user
	 * @param ctx
	 * @return Collection< YourJavaBean >
	 */
	public QueryResult query(String pvlgId, SafeUser user, Map ctx);

	/**
	 * Execute for this user, return fine grained(data level, row and column
	 * level) data. If this user has no privilege to execute privilege, return
	 * empty Collection.
	 * 
	 * @param pvlgId
	 * @param user
	 * @param ctx
	 * @param where
	 *            Customized where conditions
	 * @return Collection< YourJavaBean >
	 */
	public QueryResult query(String pvlgId, SafeUser user, Map ctx, CustomizedWhere where);

	/**
	 * Query by pagnation.
	 * 
	 * @param first
	 *            Return from this index, index counts from 0
	 * @param max
	 *            The max records be returned
	 */
	public QueryResult query(String pvlgId, SafeUser user, Map ctx, int first, int max);

	public QueryResult query(String pvlgId, SafeUser user, Map ctx, CustomizedWhere where, int first, int max);

	public int queryCount(String pvlgId, SafeUser user, Map ctx);

	public int queryCount(String pvlgId, SafeUser user, Map ctx, CustomizedWhere where);

	public void addEntitlements(String privilegeId, Collection decisionEntitlements, Collection queryEntitlements);

	public SafeQueryEntitlement addQueryEntitlement(SafeQueryEntitlement queryEntitlement) throws EntityExistException;

	public void deleteQueryEntitlement(String queryEntitlementId);

	public void updateQueryEntitlement(SafeQueryEntitlement queryEntitlement) throws EntityExistException;

	public SafeDecisionEntitlement addDecisionEntitlement(SafeDecisionEntitlement decisionEntitlement) throws EntityExistException;

	public void updateDecisionEntitlement(SafeDecisionEntitlement decisionEntitlement) throws EntityExistException;

	public void deleteDecisionEntitlement(String decisionEntitlementId);

	public void deleteCascadeEntitlementByUserCategory(String userCategoryId);

	public void deleteCascadeEntitlementByBusinessData(String businessDataId);

	public void deleteCascadeEntitlementByQuery(String queryId);

	public DecisionEntitlementTestResult testDecisionEntitlement(Locale locale, String privilegeId, List decisionEntitlements, SafeUser user, Map context);

	public QueryEntitlementTestResult testQueryEntitlement(Locale locale, String privilegeId, List queryEntitlements, SafeUser user, Map context, int first, int max);

	public SafeQuery getQuery(String privilegeId, SafeUser user, Map context);
}
